package com.zzp.basepresenter.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 时间处理
 */
public class DateUtils {
    private String ENG_DATE_FROMAT = "EEE, d MMM yyyy HH:mm:ss z";
    public String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    private String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public String YYYY_MM_DD = "yyyy-MM-dd";
    private String MM_DD = "MM-dd";
    private String YYYY = "yyyy";
    private String MM = "MM";
    private String DD = "dd";

    private static DateUtils instance;

    public static DateUtils getInstance() {
        if (instance == null) {
            synchronized (DateUtils.class) {
                if (instance == null) {
                    instance = new DateUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 秒转化为天小时分秒字符串
     *
     * @param seconds
     * @return String
     */
    public String formatSeconds(long seconds) {
        String timeStr;
        if (seconds < 10) {
            timeStr = "00:0" + seconds;
        } else {
            timeStr = "00:" + seconds;
        }
        if (seconds > 60) {
            long second = seconds % 60;
            long min = seconds / 60;
            if (second < 10) {
                if (min < 10) {
                    timeStr = "0" + min + ":0" + second;
                } else {
                    timeStr = min + ":0" + second;
                }
            } else {
                if (min < 10) {
                    timeStr = "0" + min + ":" + second;
                } else {
                    timeStr = min + ":" + second;
                }
            }
            if (min > 60) {
                min = (seconds / 60) % 60;
                long hour = (seconds / 60) / 60;
                timeStr = hour + ":" + min + ":" + second;
                if (hour > 24) {
                    hour = ((seconds / 60) / 60) % 24;
                    long day = (((seconds / 60) / 60) / 24);
                    timeStr = day + ":" + hour + ":" + min + ":" + second;
                }
            }
        }
        return timeStr;
    }


    public String generateTime(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
    }

    /**
     * 根据long毫秒数，获得时分秒
     **/
    public String getDateFormatByLong(long time) {
        int totalSeconds = (int) (time / 1000);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }

    /**
     * @param
     * @return
     * @作者
     * @创建日期
     * @创建时间
     * @描述 —— 格式化日期对象
     */
    public Date date2date(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        String str = sdf.format(date);
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            return null;
        }
        return date;
    }

    /**
     * @param
     * @return
     * @作者
     * @创建日期
     * @创建时间
     * @描述 —— 时间对象转换成字符串
     */
    public String date2string(Date date) {
        String strDate = "";
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        strDate = sdf.format(date);
        return strDate;
    }

    /**
     * 通过时间获得文件名
     *
     * @param date
     * @return
     */
    public String getFileNameByDate(Date date) {
        String strDate = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        strDate = sdf.format(date);
        return strDate;
    }

    /**
     * @param
     * @return
     * @作者
     * @创建日期
     * @创建时间
     * @描述 —— sql时间对象转换成字符串
     */
    public String timestamp2string(Timestamp timestamp) {
        String strDate = "";
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        strDate = sdf.format(timestamp);
        return strDate;
    }

    /**
     * @param dateString
     * @return
     * @作者 王建明
     * @创建日期 2012-7-13
     * @创建时间
     * @描述 —— 字符串转换成时间对象
     */
    public Date string2date(String dateString) {
        Date formateDate = null;
        DateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        try {
            formateDate = format.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
        return formateDate;
    }

    /**
     * @param date
     * @return
     * @作者
     * @创建日期
     * @创建时间
     * @描述 —— Date类型转换为Timestamp类型
     */
    public Timestamp date2timestamp(Date date) {
        if (date == null)
            return null;
        return new Timestamp(date.getTime());
    }

    /**
     * @return
     * @作者
     * @创建日期
     * @创建时间
     * @描述 —— 获得当前年份
     */
    public String getNowYear() {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY);
        return sdf.format(new Date());
    }

    /**
     * @return
     * @作者
     * @创建日期
     * @创建时间
     * @描述 —— 获得当前月份
     */
    public String getNowMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat(MM);
        return sdf.format(new Date());
    }

    /**
     * @return
     * @作者
     * @创建日期
     * @创建时间
     * @描述 —— 获得当前日期中的日
     */
    public String getNowDay() {
        SimpleDateFormat sdf = new SimpleDateFormat(DD);
        return sdf.format(new Date());
    }

    /**
     * @param time
     * @return
     * @作者
     * @创建日期
     * @创建时间
     * @描述 —— 指定时间距离当前时间的中文信息
     */
    public String getFriendlyTime(long time) {
        Calendar cal = Calendar.getInstance();
        long timel = cal.getTimeInMillis() - time;
        if (timel / 1000 < 60) {
            return "1分钟内";
        } else if (timel / 1000 / 60 < 60) {
            return timel / 1000 / 60 + "分钟前";
        } else if (timel / 1000 / 60 / 60 < 24) {
            return timel / 1000 / 60 / 60 + "小时前";
        } else {
            return timel / 1000 / 60 / 60 / 24 + "天前";
        }
    }

    /**
     * 以友好的方式显示时间
     *
     * @param time
     * @return
     */
    public String getFriendlyTime(Date time) {
        //获取time距离当前的秒数  
        int ct = (int) ((System.currentTimeMillis() - time.getTime()) / 1000);
        if (ct == 0) {
            return "刚刚";
        }
        if (ct > 0 && ct < 60) {
            return ct + "秒前";
        }
        if (ct >= 60 && ct < 3600) {
            return Math.max(ct / 60, 1) + "分钟前";
        }
        if (ct >= 3600 && ct < 86400)
            return ct / 3600 + "小时前";
        if (ct >= 86400 && ct < 2592000) { //86400 * 30
            int day = ct / 86400;
            return day + "天前";
        }
        if (ct >= 2592000 && ct < 31104000) { //86400 * 30
            return ct / 2592000 + "月前";
        }
        return ct / 31104000 + "年前";
    }

    /**
     * 格式化日期字符串
     *
     * @param currentTime
     * @return
     */
    public String formatString(String currentTime) {
        DateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        return format.format(currentTime);
    }

    public String stringForTime(long position) {
        int totalSeconds = (int) ((position / 1000.0) + 0.5);
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;
        return String.format(Locale.US, "%02d:%02d:%02d", hours, minutes, seconds).toString();
    }

    // date类型转换为String类型
    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    // data Date类型的时间
    public String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }


    // long转换为Date类型
    // currentTime要转换的long类型的时间
    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    public Date longToDate(long currentTime, String formatType)
            throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }

    // long类型转换为String类型
    // currentTime要转换的long类型的时间
    // formatType要转换的string类型的时间格式
    public String longToString(long currentTime, String formatType)
            throws ParseException {
        Date date = longToDate(currentTime, formatType); // long类型转成Date类型
        String strTime = dateToString(date, formatType); // date类型转成String
        return strTime;
    }

    // string类型转换为date类型
    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }


    public String StringData() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mMonth + "月" + mDay + "日" + "   星期" + mWay;
    }


    /*小时差*/
    public boolean timeCount(long currentTime, long oldtime) {
        try {
            Date fromDate2 = longToDate(oldtime, YYYY_MM_DD_HH_MM_SS);
            Date toDate2 = longToDate(currentTime, YYYY_MM_DD_HH_MM_SS);
            long from2 = fromDate2.getTime();
            long to2 = toDate2.getTime();
            int hours = (int) ((to2 - from2) / (1000 * 60 * 60));
            return hours >= 12;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }
}
